package backtrace;

import java.util.ArrayList;
import java.util.List;

public class leetcode77组合 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(result, list, n, k);
        return result;
    }

    public void dfs(List<List<Integer>> result, List<Integer> list, int n, int k) {
        if (k == 0) {
            result.add(new ArrayList<>(list));
        } else {
            int size = list.size();
            int begin = size == 0 ? 0 : list.get(size - 1);
            for (int i = begin; i < n; i++) {
                list.add(i + 1);
                dfs(result, list, n, k - 1);
                k++;
                Integer obj = i + 1;
                list.remove(obj);
            }
        }
    }
}
